function loadPage() {
    chrome.tabs.query({
            url: whatsAppUrl
        },
        function(a) {
            if (a.length > 0) {
                chrome.tabs.update(a[0].id, {
                    active: !0
                });
                var b = "window.location.reload();";
                chrome.tabs.executeScript(a[0].id, {
                    code: b
                })
            } else isWhatsDock || loggedLogic()
        })
}

function updateWhatsAppTabs(a) {
    chrome.tabs.query({
            url: whatsAppUrl + "*"
        },
        function(b) {
            whatsAppTabs = [];
            for (var c = 0; c < b.length; c++) whatsAppTabs.push(b[c].id);
            debug && console.info("WANote: Updated WhatsApp tabs: " + JSON.stringify(whatsAppTabs)),
                a()
        })
}

function closeAllWhatsAppTabs() {
    return closeAllWhatsAppTabsBut(-1)
}

function closeAllWhatsAppTabsBut(a) {
    for (var b = [], c = whatsAppTabs.length - 1; c >= 0; c--) {
        var d = whatsAppTabs[c];
        d != a && b.push(whatsAppTabs.splice(c, 1)[0])
    }
    if (b.length > 0) try {
        chrome.tabs.remove(b)
    } catch (e) {}
    return b.length
}

function onMessage(a, b, c) {
    if ("getIsBackgroundPage" == a.name) c(void 0 == b.tab);
    else if ("setToolbarIcon" == a.name) a.warn ? (chrome.browserAction.setIcon({
            path: {
                19: "img/favicon19warn.png",
                38: "img/favicon38warn.png"
            }
        }), islogged = !1) : (chrome.browserAction.setIcon({
            path: {
                19: "img/favicon19.png",
                38: "img/favicon38.png"
            }
        }), islogged = !0),
        void 0 != a.badgeText && (chrome.browserAction.setBadgeBackgroundColor({
            color: [13, 193, 67, 255]
        }), chrome.browserAction.setBadgeText({
            text: a.badgeText
        })),
        void 0 != a.tooltipText && chrome.browserAction.setTitle({
            title: a.tooltipText
        });
    else if ("backgroundNotificationClicked" == a.name) {
        var d = whatsAppUrl + ("string" == typeof a.srcChatTitle ? sourceChatFragment + encodeURIComponent(a.srcChatTitle) : "");
        chrome.windows.getCurrent(function(a) {
            void 0 == a || void 0 == a.id || isWhatsDock ? chrome.runtime.lastError && !isWhatsDock && (debug && console.info("WANote: Create new WhatsApp tab in new window"), chrome.windows.create({
                    url: d,
                    focused: !0
                },
                function(a) {
                    chrome.tabs.query({
                            url: whatsAppUrl + "*",
                            active: !1
                        },
                        function(a) {
                            for (var b = [], c = 0; c < a.length; c++) b.push(a[c].id);
                            b.length > 0 && chrome.tabs.remove(b)
                        })
                })) : (debug && console.info("WANote: Create new WhatsApp tab in current window"), chrome.tabs.create({
                    windowId: a.id,
                    url: d
                },
                function(a) {
                    chrome.windows.update(a.windowId, {
                        focused: !0
                    })
                }))
        })
    } else if ("getOptions" == a.name) "true" == localStorage.whatsnoteBackgroundNotif || "false" == localStorage.whatsnoteBackgroundNotif ? backgroundNotif = "true" == localStorage.whatsnoteBackgroundNotif : localStorage.whatsnoteBackgroundNotif = backgroundNotif,
        "true" == localStorage.whatsnoteWideText || "false" == localStorage.whatsnoteWideText ? wideText = "true" == localStorage.whatsnoteWideText : localStorage.whatsnoteWideText = wideText,
        c({
            backgroundNotif: backgroundNotif,
            wideText: wideText
        });
    else if ("setOptions" == a.name) void 0 != a.backgroundNotif && (backgroundNotif = 1 == a.backgroundNotif, localStorage.whatsnoteBackgroundNotif = backgroundNotif),
        void 0 != a.wideText && (wideText = 1 == a.wideText, localStorage.whatsnoteWideText = wideText);
    else if ("tryToLoggin" == a.name) loggedLogic(!0, !0);
    else if ("tryToLogginActive" == a.name) loggedLogic(!0, !0);
    else if ("content" == a.from && "channels" == a.subject) localStorage.channels = JSON.stringify(a.channels);
    else if ("content" == a.from && "avatar" == a.subject) "undefined" != typeof a.number && (mynumber = a.number),
        "undefined" != typeof a.avatar && (myavatar = a.avatar),
        watabid = b.tab.id,
        whatwindowid = b.tab.windowId,
        localStorage.avatar = a.avatar,
        localStorage.number = a.number;
    else if ("sendtoWhatsDock" == a.name) chrome.runtime.sendMessage(whatsdockid, {
                message: a.message,
                mynumber: a.mynumber
            },
            function(a) {
                a.msglist && a.msglist.length > 0 && (localStorage.mymsg = JSON.stringify(a.msglist))
            }),
        c({
            status: "ok"
        });
    else if ("deletemsgWhatsDock" == a.name) chrome.runtime.sendMessage(whatsdockid, {
                deletemsg: a.deletemsg
            },
            function(a) {}),
        c({
            status: "ok"
        });
    else if ("focusWhatsDock" == a.name) chrome.runtime.sendMessage(whatsdockid, {
            focus: !0
        },
        function(a) {});
    else if ("focusWhatsApp" == a.name) loggedLogic(!0, !0),
        watabid > 0 && (chrome.windows.update(wawindowid, {
            focused: !0
        }), chrome.tabs.update(watabid, {
            active: !0
        }));
    else if ("myChats" == a.name) {
        if (a.id == mynumber + "@c.us") {
            var e = [],
                f = JSON.parse(a.chats);
            for (i = 0; i < f.length; i++) e.push({
                type: f[i].type,
                body: f[i].body,
                id: f[i].id.id
            });
            localStorage.mymsg = JSON.stringify(e),
                "undefined" != typeof a.cid && (localStorage.mycid = a.cid)
        }
    } else if ("sendmessagefromsender" == a.name) {
        var c = function() {
            chrome.runtime.lastError ? (loggedLogic(!0, !0), watabid > 0 && ("" == a.msg && (0 !== whatwindowid && chrome.windows.update(whatwindowid, {
                    focused: !0
                },
                function(a) {}), chrome.tabs.update(watabid, {
                    active: !0
                },
                function(a) {})), a.wdsource ? chrome.tabs.sendMessage(watabid, {
                    message: a.msg,
                    mynumber: a.number,
                    wdsource: a.wdsource
                },
                function(a) {}) : chrome.tabs.sendMessage(watabid, {
                    message: a.msg,
                    mynumber: a.number
                },
                function(a) {}))) : ("" == a.msg && (0 !== whatwindowid && chrome.windows.update(whatwindowid, {
                    focused: !0
                },
                function(a) {}), chrome.tabs.update(watabid, {
                    active: !0
                },
                function(a) {})), a.wdsource ? chrome.tabs.sendMessage(watabid, {
                    message: a.msg,
                    mynumber: a.number,
                    wdsource: a.wdsource
                },
                function(a) {}) : chrome.tabs.sendMessage(watabid, {
                    message: a.msg,
                    mynumber: a.number
                },
                function(a) {}))
        };
        chrome.tabs.get(watabid, c)
    } else if ("showwdinfowindow" == a.name) {
        var c = function() {
            chrome.runtime.lastError ? (loggedLogic(!0, !0), watabid > 0 && (chrome.tabs.update(watabid, {
                    active: !0
                },
                function(a) {}), chrome.tabs.sendMessage(watabid, {
                    showinfowindow: !0
                },
                function(a) {}))) : (chrome.tabs.update(watabid, {
                    active: !0
                },
                function(a) {}), chrome.tabs.sendMessage(watabid, {
                    showinfowindow: !0
                },
                function(a) {}))
        };
        0 !== whatwindowid && chrome.windows.update(whatwindowid, {
                    focused: !0
                },
                function(a) {}),
            chrome.tabs.get(watabid, c)
    } else if ("getcontactexists" == a.name) {
        var c = function() {
            chrome.runtime.lastError ? (loggedLogic(!0, !0), watabid > 0 && chrome.tabs.sendMessage(watabid, {
                    getcontactexists: a.id,
                    tabid: b.tab.id
                },
                function(a) {})) : chrome.tabs.sendMessage(watabid, {
                    getcontactexists: a.id,
                    tabid: b.tab.id
                },
                function(a) {})
        };
        chrome.tabs.get(watabid, c)
    } else "getcontactexistsdone" == a.name ? chrome.tabs.sendMessage(a.tabid, {
            getcontactexistsdone: a.id,
            exists: a.exists
        },
        function(a) {}) : "getsetRate" == a.name ? ("undefined" != typeof a.setrate && (setrate = a.setrate, localStorage.wdrate = setrate), chrome.tabs.sendMessage(b.tab.id, {
            getRate: setrate
        },
        function(a) {})) : "setopenindock" == a.name ? chrome.windows.get(b.tab.windowId,
        function(a) {
            "panel" !== a.type && "popup" !== a.type ? chrome.tabs.sendMessage(b.tab.id, {
                    changewelcolmelogo: !0
                },
                function(a) {}) : chrome.windows.update(a.id, {
                focused: !0
            })
        }) : "openindock" == a.name && chrome.windows.create({
            url: "https://web.whatsapp.com",
            type: "panel",
            width: parseInt(b.tab.width / 2),
            height: parseInt(.75 * b.tab.height)
        },
        function(a) {});
    return !0
}

function loggedLogic(a, b) {
    "undefined" == typeof a && (a = !0),
        "undefined" == typeof b && (b = !0),
        islogged ? (chrome.browserAction.setIcon({
            path: {
                19: "img/favicon19.png",
                38: "img/favicon38.png"
            }
        }), a && chrome.tabs.query({
                url: whatsAppUrl
            },
            function(a) {
                a.length > 0 ? (watabid = a[0].id, wawindowid = a[0].windowId, chrome.windows.update(wawindowid, {
                    focused: !0
                }), chrome.tabs.update(a[0].id, {
                    active: !0
                })) : (watabid = 0, wawindowid = 0, chrome.tabs.create({
                    url: whatsAppUrl,
                    active: b
                }))
            })) : chrome.runtime.sendMessage(whatsdockid, {
                whatsnote: !0
            },
            function(c) {
                "undefined" == typeof c ? (isWhatsDock = !1, chrome.browserAction.setIcon({
                    path: {
                        19: "img/favicon19warn.png",
                        38: "img/favicon38warn.png"
                    }
                }), a && chrome.tabs.query({
                        url: whatsAppUrl
                    },
                    function(a) {
                        if (a.length > 0) {
                            watabid = a[0].id,
                                wawindowid = a[0].windowId,
                                chrome.windows.update(wawindowid, {
                                    focused: !0
                                }),
                                chrome.tabs.update(a[0].id, {
                                    active: !0
                                });
                            var c = "window.location.reload();";
                            chrome.tabs.executeScript(a[0].id, {
                                code: c
                            })
                        } else wawindowid = 0,
                            watabid = 0,
                            chrome.tabs.create({
                                url: whatsAppUrl,
                                active: b
                            })
                    })) : (isWhatsDock = !0, b ? chrome.runtime.sendMessage(whatsdockid, {
                        focus: !0
                    },
                    function(a) {}) : chrome.runtime.sendMessage(whatsdockid, {},
                    function(a) {
                        a.mynum && (mynumber = a.mynum),
                            a.myavatar && (myavatar = a.myavatar),
                            a.running && (islogged = !0)
                    }))
            })
}
var debug = !1,
    whatsAppUrl = "https://web.whatsapp.com/",
    newTabUrl = "chrome://newtab/",
    sourceChatFragment = "#watSrcChatTitle=",
    backgroundNotif = !0,
    wideText = !1,
    islogged = !1,
    mynumber = "",
    myavatar = "",
    watabid = 0,
    whatwindowid = 0,
    isWhatsDock = !1,
    whatsdockid = "ggkoaeeeojgdhkpalgklpddfdolnmjpi_deactivated",
    setrate = !1,
    isBackgroundPageLoaded = !1,
    whatsAppTabs = [];
updateWhatsAppTabs(function() {
        0 == whatsAppTabs.length ? (debug && console.info("WANote: There were no WhatsApp tabs on startup, try to enable it"), loadPage()) : 1 == whatsAppTabs.length ? debug && console.info("WANote: There was one WhatsApp tab on startup, do nothing") : (debug && console.info("WANote: There were more than one WhatsApp tabs on startup, close all but the last one"), closeAllWhatsAppTabsBut(whatsAppTabs[whatsAppTabs.length - 1]))
    }),
    chrome.runtime.onInstalled.addListener(function(a) {
        localStorage.mymsg = "",
            localStorage.mycid = "",
            "install" == a.reason ? updateWhatsAppTabs(function() {
                var a = closeAllWhatsAppTabs();
                a > 0 ? (debug && console.info("WANote: There were WhatsApp tabs on install, open a new one"), loggedLogic()) : (debug && console.info("WANote: There were no WhatsApp tabs on install, set it as unlogged"), islogged = !1, loggedLogic(!1))
            }) : "update" == a.reason && ("undefined" == typeof localStorage.wdrate ? (setrate = !0, localStorage.wdrate = !0) : setrate = localStorage.wdrate)
    }),
    chrome.tabs.onUpdated.addListener(function(a, b, c) {
        "string" == typeof b.url && (0 == b.url.indexOf(whatsAppUrl) && whatsAppTabs.indexOf(a) == -1 ? (debug && console.info("WANote: New WhatsApp tab, close all other WhatsApp tabs or background page"), whatsAppTabs.push(a), closeAllWhatsAppTabsBut(a)) : 0 != b.url.indexOf(whatsAppUrl) && whatsAppTabs.indexOf(a) > -1 && (debug && console.info("WANote: 'Closed' the only WhatsApp tab, set it as unlogged"), whatsAppTabs.splice(whatsAppTabs.indexOf(a), 1), islogged = !1, loggedLogic(!1)))
    }),
    chrome.tabs.onRemoved.addListener(function(a, b) {
        whatsAppTabs.indexOf(a) > -1 && (debug && console.info("WANote: Closed the only WhatsApp tab"), whatsAppTabs.splice(whatsAppTabs.indexOf(a), 1), islogged = !1, loggedLogic(!1))
    }),
    chrome.browserAction.onClicked.addListener(function(a) {
        chrome.tabs.query({
                url: whatsAppUrl + "*",
                lastFocusedWindow: !0
            },
            function(b) {
                b.length > 0 ? chrome.tabs.update(b[0].id, {
                    active: !0
                }) : a.url == newTabUrl ? chrome.tabs.update(a.id, {
                    url: whatsAppUrl
                }) : isWhatsDock || chrome.tabs.create({
                    url: whatsAppUrl
                })
            })
    }),
    chrome.runtime.onMessage.addListener(onMessage),
    chrome.runtime.onMessageExternal.addListener(function(a, b, c) {
        if (response = {
                status: "ok"
            },
            a.mynumber) "undefined" == typeof a.mynumber || "" == a.mynumber ? (islogged = !1, isWhatsDock = !1, mynumber = "", chrome.browserAction.setIcon({
            path: {
                19: "img/favicon19warn.png",
                38: "img/favicon38warn.png"
            }
        })) : (chrome.browserAction.setIcon({
            path: {
                19: "img/favicon19.png",
                38: "img/favicon38.png"
            }
        }), islogged = !0, mynumber = a.mynumber, a.avatar && (myavatar = a.avatar));
        else if (a.mychats) {
            if (a.id == mynumber + "@c.us") {
                var d = [],
                    e = JSON.parse(a.mychats);
                for (i = 0; i < e.length; i++) d.push({
                    type: e[i].type,
                    body: e[i].body,
                    id: e[i].id.id
                });
                localStorage.mymsg = JSON.stringify(d),
                    "undefined" != typeof a.cid && (localStorage.mycid = a.cid)
            }
        } else "undefined" != typeof a.unreadcount ? (chrome.browserAction.setBadgeBackgroundColor({
            color: [13, 193, 67, 255]
        }), 0 == a.unreadcount ? chrome.browserAction.setBadgeText({
            text: ""
        }) : chrome.browserAction.setBadgeText({
            text: a.unreadcount.toString()
        })) : (islogged = !1, isWhatsDock = !1, chrome.browserAction.setIcon({
            path: {
                19: "img/favicon19warn.png",
                38: "img/favicon38warn.png"
            }
        }));
        c(response)
    }),
    loggedLogic();